package com.borya.util;

import com.borya.framework.core.ServerSqlSession;
import com.borya.framework.core.ServerSqlSessionFactory;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * 保存app配置信息
 *
 * @author borya
 *
 */
public final class AppConf {

	private Logger log = Logger.getLogger(getClass());
	public static Map<String,String> confMap;
	public static int detailType = Constant.detailType;
	private ServerSqlSessionFactory sqlDataSource;
	private AppConf() {
	}

	private static final class UserCheckManagerHolder {
		private final static AppConf instance = new AppConf();
	}

	public static final AppConf getInstance() {
		return UserCheckManagerHolder.instance;
	}

	private ServerSqlSessionFactory getSqlDataSource() {
		return sqlDataSource;
	}


	public void init(ServerSqlSessionFactory sqlDataSource) {
		this.sqlDataSource = sqlDataSource;

		setConf();
		log.info("配置表："+confMap.toString());
	}





	private void setConf() {
		try (ServerSqlSession session = getSqlDataSource().session();) {
			StringBuilder sb = new StringBuilder(128);
			sb.append("select default_table,self_table from tf_app_table_").append(detailType);
			session.executeQuery(sb.toString(), (rs)->{
				if(rs != null){
					confMap = new HashMap<>(8);
					while(rs.next()){
						confMap.put(rs.getString(1),rs.getString(2));
					}
				}
				return null;
			});
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
	}


}